/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: zhnb
 * Date: 2011-4-11 下午01:57:40
 */

package com.ccx.memc;

import java.io.FileNotFoundException;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Properties;
import java.util.PropertyResourceBundle;

import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 配置文件加载器
 * 
 * 使用公司统一的内存缓存机制，调整了读取配置文件的方法
 * modify by zhaoshujin 2011-06-18
 * 
 */
public class MemcConfigLoader implements Serializable {

	private static final long serialVersionUID = 707845616525487524L;
	private static Logger logger = LoggerFactory.getLogger(MemcConfigLoader.class);

	static private String path = "memc";
	private static Properties sysProperty = null;

	/**
	 * 获取配置信息
	 * 
	 * @param key
	 * @return
	 */
	public static String getProperty(String key) {

		return getProperty(key, "");
	}

	public static String getProperty(String key, String defaultValue) {

		if (sysProperty == null) {
			sysProperty = getPropObjFromBundle(path);
		}

		return sysProperty.getProperty(key, defaultValue);
	}

	public static int getProperty(String key, int value) {
		String val = getProperty(key, value + "");
		return (val == null) ? value : Integer.parseInt(val);
	}

	public static Properties getPropObjFromBundle(String bundleName) {

		Properties objProp = new Properties();

		PropertyResourceBundle bundle = (PropertyResourceBundle) PropertyResourceBundle.getBundle(bundleName);
		Enumeration<?> enm = bundle.getKeys();
		while (enm.hasMoreElements()) {
			String key = (String) enm.nextElement();
			String value = bundle.getString(key);
			objProp.setProperty(key, value);

		}
		return objProp;
	}
	
	private MemcConfigLoader() {
	}

	/**
	 * 按默认名称的文件初始化
	 * 
	 * @throws FileNotFoundException
	 * @throws DocumentException
	 */
	public static MemcConfigInfo init2() throws FileNotFoundException, DocumentException {
		return init();
	}
	
	public static MemcConfigInfo init() throws FileNotFoundException, DocumentException {

		// 待返回配置信息对象
		MemcConfigInfo mci = new MemcConfigInfo();
		
		// 读取配置信息
		String Servers = MemcConfigLoader.getProperty("servers");
		String Weights = MemcConfigLoader.getProperty("weights");

		String InitConn = MemcConfigLoader.getProperty("initConn");
		String MinConn = MemcConfigLoader.getProperty("minConn");
		String MaxConn = MemcConfigLoader.getProperty("maxConn");
		String MaxIdle = MemcConfigLoader.getProperty("maxIdle");
		String MaintSleep = MemcConfigLoader.getProperty("maintSleep");

		String Nagle = MemcConfigLoader.getProperty("nagle");
		String SocketTO = MemcConfigLoader.getProperty("socketTO");
		String SocketConnectTO = MemcConfigLoader.getProperty("socketConnectTO");

		String CompressEnable = MemcConfigLoader.getProperty("compressEnable");
		String CompressThreshold = MemcConfigLoader.getProperty("compressThreshold");
		
		// 整理配置信息
		String[] servers = (Servers == null) ? null : Servers.split(";");
		Integer[] weights = null;
		if (Weights != null) {
			String[] weightArr = Weights.split(";");
			weights = new Integer[weightArr.length];
			for (int i = 0; i < weights.length; i++) {
				weights[i] = Integer.parseInt(weightArr[i]);
			}
		}

		int initConn = (InitConn == null) ? 5 : Integer.parseInt(InitConn);
		int minConn = (InitConn == null) ? 5 : Integer.parseInt(MinConn);
		int maxConn = (InitConn == null) ? 250 : Integer.parseInt(MaxConn);
		long maxIdle = (InitConn == null) ? 360 * 60 * 1000 : Integer.parseInt(MaxIdle) * 60 * 1000;

		int maintSleep = (InitConn == null) ? 30 : Integer.parseInt(MaintSleep);

		boolean nagle = (Nagle == null) ? false : (Nagle.equalsIgnoreCase("true") ? true : false);

		int socketTO = (SocketTO == null) ? 3000 : Integer.parseInt(SocketTO);
		int socketConnectTO = (SocketConnectTO == null) ? 0 : Integer.parseInt(SocketConnectTO);

		boolean compressEnable = (CompressEnable == null) ? false : (CompressEnable.equalsIgnoreCase("true") ? true : false);
		int compressThreshold = (CompressThreshold == null) ? 64 * 1024 : Integer.parseInt(CompressThreshold) * 1024;

		// 设置参数到配置信息对象
		mci.setServers(servers);
		mci.setWeights(weights);
		mci.setInitConn(initConn);
		mci.setMinConn(minConn);
		mci.setMaxConn(maxConn);
		mci.setMaxIdle(maxIdle);
		mci.setMaintSleep(maintSleep);
		mci.setNagle(nagle);
		mci.setSocketTO(socketTO);
		mci.setSocketConnectTO(socketConnectTO);
		mci.setCompressEnable(compressEnable);
		mci.setCompressThreshold(compressThreshold);

		// 调试阶段日志
		logger.debug((mci == null) ? null : mci.toString());

		return mci;
		
	}

}
